package sg.edu.nus.iss.vmcs.machinery;

/*
 * Copyright 2011 ISS.
 * The contents contained in this document may not be reproduced in any
 * form or by any means, without the written permission of ISS, other
 * than for the purpose for which it has been supplied.
 *
 */

import sg.edu.nus.iss.vmcs.system.*;
import sg.edu.nus.iss.vmcs.util.*;
import sg.edu.nus.iss.vmcs.store.*;

/**
 * This class represents the controller object of Machinery function which interacts
 * with the user interface object for machinery simulation and the entity objects.
 * It is also responsible for instantiating the door object of VMCS. This controller
 * object gets instantiated by the {@link MainController}
 * 
 *
 * @version 1.0 7/12/2011
 * @author Team 3S SE16
 */

public class MachineryController {

	public MainController mainCtrl;
	public StoreController storeCtrl;

	private MachinerySimulatorPanel ml;
	private Door door;

/**
 * This constructor creates a new instance of the MachineryController.  
 * @param mctrl Reference to the instance of the MainController class.
 * @see MainController
 */
	
	public MachineryController(MainController mctrl) {
		mainCtrl = mctrl;
		storeCtrl = mctrl.getStoreController();
	}

/**
 * This method returns the reference to the instance of MainController class.  
 * @return mainCtrl Reference to the instance of the MainController class.
 * @see MainController
 */
	
	public MainController getMainController() {
		return mainCtrl;
	}

/**
 * This method creates the new instance of the Door class.  
 * @throws VMCSException Error during creation of the Door object
 * @see MainController#initialize()
 */
	
	public void initialize() throws VMCSException {
		door = new Door();
	}

/**
 * This method disposes the instance of {@link MachinerySimulatorPanel}.  
 */
	
	public void closeDown() {
		if (ml != null)
			ml.dispose();
	}

	/* ************************************************************
	 * Panel Operations
	 */

/**
 * This method display the Machinery simulator panel.
 * @see MachinerySimulatorPanel  
 */
	
	public void displayMachineryPanel() {
		SimulatorControlPanel scp = mainCtrl.getSimulatorControlPanel();
		if (ml == null)
			ml = new MachinerySimulatorPanel(scp, this);
		ml.display();
		//System.out.println("get door status:" + door.isDoorClosed());
		scp.setActive(SimulatorControlPanel.ACT_MACHINERY, false);
	}

/**
 * This method checks and disposes Machinery simulator panel. Before 
 * closing it checks state of the Door object whether it is closed or not.
 * @see MachinerySimulatorPanel  
 */
	
	public void closeMachineryPanel() {
		if (ml == null)
			return;
		boolean ds = isDoorClosed();

		if (ds == false) {
			MessageDialog msg =
				new MessageDialog(ml, "Please Lock the Door before You Leave");
			msg.setVisible(true);
			return;
		}
		ml.dispose();
		SimulatorControlPanel scp = mainCtrl.getSimulatorControlPanel();
		scp.setActive(SimulatorControlPanel.ACT_MACHINERY, true);
	}

	/* ************************************************************
	 * Door operation
	 */

/**
 * This method returns the state of the door whether closed or opened
 * @return boolean status of the door closed
 * @see Door  
 */
	
	public boolean isDoorClosed() {
		return door.isDoorClosed();
	}

/**
 * This method set state of the door either close or open
 * @param state Door Close or Open
 * @see Door  
 */
	
	public void setDoorState(boolean state) {
		//System.out.println("StoreController.setDoorState:" + state);
		door.setState(state);
		displayDoorState();
	}

	/* ************************************************************
	 * Display operation
	 */

/**
 * This method display the state of the door on the machinery simulation panel.
 * @see MachinerySimulatorPanel#setDoorState(boolean)  
 */
	public void displayDoorState() {
		if (ml == null)
			return;
		ml.setDoorState(door.isDoorClosed());
	}

/**
 * This method updates drinks stock view on the machinery simulation panel.
 * @throws VMCSException Error during updating of drink stock view.
 * @see MachinerySimulatorPanel#getDrinksStoreDisplay() 
 */
	
	// update drink stock view;
	public void displayDrinkStock() throws VMCSException {
		if (ml == null)
			return;
		ml.getDrinksStoreDisplay().update();
	}

/**
 * This method updates coin stock view on the machinery simulation panel
 * after transfer all cash.
 * @throws VMCSException Error during updating of coin stock view.
 * @see MachinerySimulatorPanel#getCashStoreDisplay()
 */
	
	// update coin stock view after transfer all cash;
	public void displayCoinStock() throws VMCSException {
		if (ml == null)
			return;
		ml.getCashStoreDisplay().update();
	}

	/* ************************************************************
	 * Interactions with the Store that need to update the display
	 */

/**
 * This method interacts with store controller for updating cash store
 * that need to update the display
 * @throws VMCSException Error during updating of coin stock view.
 * @see MachinerySimulatorPanel#getCashStoreDisplay()
 * @see StoreController#storeCoin(Coin)
 */
	
	public void storeCoin(Coin c) throws VMCSException {
		storeCtrl.storeCoin(c);
		if (ml != null)
			ml.getCashStoreDisplay().update();
	}

/**
 * This method interacts with store controller for updating drinks store
 * that need to update the display
 * @throws VMCSException Error during updating of drinks stock view.
 * @see MachinerySimulatorPanel#getDrinksStoreDisplay()
 * @see StoreController#dispenseDrink(int)
 */
	
	public void dispenseDrink(int idx) throws VMCSException {
		storeCtrl.dispenseDrink(idx);
		if (ml != null)
			ml.getCashStoreDisplay().update();

	}
	
/**
 * This method interacts with store controller for giving change
 * and update the coin stock view
 * @throws VMCSException Error during updating of drinks stock view.
 * @see MachinerySimulatorPanel#getCashStoreDisplay()
 * @see StoreController#giveChange(int, int)
 */
	
	public void giveChange(int idx, int numOfCoins) throws VMCSException {
		
		storeCtrl.giveChange(idx, numOfCoins);
		if (ml != null)
			ml.getCashStoreDisplay().update();
    }

}
